<template>
  <div>
       <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入要查询的商品名称" v-model="queryInfo.query"   @keyup.enter.native="getGoodsList('search')">
            <template #append><el-button @click="getGoodslist('search')">搜索</el-button></template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table border :data="goodsList" stripe>
        <el-table-column label="#" type="index"></el-table-column>

        <el-table-column label="商品名称">
          <template #default="scope">{{ scope.row.goods_name}}</template>
        </el-table-column>

        <el-table-column label="商品价格(元)" width="95">
          <template #default="scope">{{ scope.row.goods_price}}</template>
        </el-table-column>

        <el-table-column label="商品重量(kg)" width="100">
          <template #default="scope">{{ scope.row.goods_weight}}</template>
        </el-table-column>

        <el-table-column label="创建时间" width="150">
          <template #default="scope">{{ $filters.dateFormat(scope.row.upd_time)}}</template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button type="primary" size="small">编辑</el-button>
            <el-button type="danger" size="small" @click="removeById(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        v-model:currentPage="queryInfo.pagenum"
        v-model:page-size="queryInfo.pagesize"
        :page-sizes="[5, 10, 15, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import axios from '../../http/axios.js'

export default {
  name: 'List',
  data(){
    return{
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [], //商品列表数据
      total: 0
    }
  },
  created(){
    this.getGoodsList();

  },
  methods: {
    async getGoodsList(search){ // 获取商品列表
      if(search){
        if(!this.queryInfo.query){
          return;
        }
      };


      const { data: res} = await axios.get('goods',{params: this.queryInfo});

      if(res.meta.status !== 200){
        this.$message({
          type: 'error',
          message: '获取商品列表失败'
        })
      }else{
        this.goodsList = res.data.goods;
        this.total = res.data.total;
      }
    },
    async removeGoodsById(goodsId){ //根据id获取商品
      const {data: res} = await axios.delete(`goods/${goodsId}`);

      if(res.meta.status !== 200){
        this.$message({
          type: 'error',
          message: '删除失败，服务器运行错误'
        });
      }else {
        this.$message({
          type: 'success',
          message: '删除成功'
        });
        this.getGoodsList();
      }
    },
    handleSizeChange(newPagesize){// 该表获取数据的总条数
      this.queryInfo.pagesize = newPagesize;
      this.getGoodsList();
      
    },
    handleCurrentChange(newPagenum){ // 改变数据页数
      this.queryInfo.pagenum = newPagenum;
      this.getGoodsList();
    },
    removeById(goodsId){
      this.$messageBox.confirm('确认要永久删除该产品吗?','删除商品',{
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.removeGoodsById(goodsId);
      }).catch(()=>{})
    },
    goAddPage(){
      this.$router.push('/goods/add');
    }
  }
}
</script>

<style>
</style>